package org.linjie.cocacola.yiluchunyue2019.service;


import com.emallos.xiama.dto.DonationRecordDto;
import com.emallos.xiama.model.DonationRecordModel;
import com.emallos.xiama.repository.DonationRecordRepository;
import com.emallos.xiama.support.repository.BaseRepository;
import com.emallos.xiama.support.repository.MyCriteriaParams;
import com.emallos.xiama.support.repository.PageResult;
import com.emallos.xiama.support.repository.hibernate.CriteriaParams;
import com.emallos.xiama.support.service.SupportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.math.BigDecimal;
import java.util.List;


/**
 *  ,服务类
 * @author cnlinjie@foxmail.com
 */
@Service
public class DonationRecordService  extends SupportService<DonationRecordModel,Long> {

    private final static Logger logger = LoggerFactory.getLogger(DonationRecordService.class);

    @Autowired
    private DonationRecordRepository repository;

    /**
     * 分页查询
     * @param dto 分页查询参数
     * @return
     */
    public PageResult page(DonationRecordDto dto) {
        CriteriaParams params = MyCriteriaParams.get();
        if (!Strings.isNullOrEmpty(dto.getKeyword())) {
         //   params.add(Restrictions.like("keyword", "%" + dto.getKeyword() + "%"));
        }
        return allOrPage(params, dto);
    }

    /**
     * 创建
     */
    @Transactional
    public Long save(DonationRecordDto dto) {
        DonationRecordModel model = new DonationRecordModel(dto.getTargetSourceType() ,dto.getTargetId() ,dto.getTargetName() ,dto.getTargetHeadImg() ,dto.getFromSourceUserId() ,dto.getUserId() ,dto.getUserRealname() ,dto.getUserWxNickname() ,dto.getUserWxHeadImg() ,dto.getRealname() ,dto.getLeaveMessage() ,dto.getMoney() ,dto.getMoneyFloat() ,dto.getNeedInvoice() ,dto.getInvoiceTitle() ,dto.getInvoiceAddress() ,dto.getInvoiceContactName() ,dto.getInvoiceContactPhone() ,dto.getPayStatus() ,dto.getReturnCode() ,dto.getReturnMsg() ,dto.getAppid() ,dto.getMchId() ,dto.getDeviceInfo() ,dto.getNonceStr() ,dto.getSign() ,dto.getSignType() ,dto.getResultCode() ,dto.getErrCode() ,dto.getErrCodeDes() ,dto.getOpenid() ,dto.getIsSubscribe() ,dto.getTradeType() ,dto.getBankType() ,dto.getTotalFee() ,dto.getSettlementTotalFee() ,dto.getFeeType() ,dto.getCashFee() ,dto.getCashFeeType() ,dto.getCouponFee() ,dto.getTransactionId() ,dto.getOutTradeNo() ,dto.getAttach() ,dto.getTimeEnd() ,dto.getPayNote() );
        repository.save(model);
        return model.getId();
    }

    /**
     * 全量更新
     */
    @Transactional
    public void update(DonationRecordDto dto) {
        DonationRecordModel model = getById(dto.getId());
        model.update(
            dto.getTargetSourceType() ,dto.getTargetId() ,dto.getTargetName() ,dto.getTargetHeadImg() ,dto.getFromSourceUserId() ,dto.getUserId() ,dto.getUserRealname() ,dto.getUserWxNickname() ,dto.getUserWxHeadImg() ,dto.getRealname() ,dto.getLeaveMessage() ,dto.getMoney() ,dto.getMoneyFloat() ,dto.getNeedInvoice() ,dto.getInvoiceTitle() ,dto.getInvoiceAddress() ,dto.getInvoiceContactName() ,dto.getInvoiceContactPhone() ,dto.getPayStatus() ,dto.getReturnCode() ,dto.getReturnMsg() ,dto.getAppid() ,dto.getMchId() ,dto.getDeviceInfo() ,dto.getNonceStr() ,dto.getSign() ,dto.getSignType() ,dto.getResultCode() ,dto.getErrCode() ,dto.getErrCodeDes() ,dto.getOpenid() ,dto.getIsSubscribe() ,dto.getTradeType() ,dto.getBankType() ,dto.getTotalFee() ,dto.getSettlementTotalFee() ,dto.getFeeType() ,dto.getCashFee() ,dto.getCashFeeType() ,dto.getCouponFee() ,dto.getTransactionId() ,dto.getOutTradeNo() ,dto.getAttach() ,dto.getTimeEnd() ,dto.getPayNote() 
        );
        repository.save(model);
    }

    @Transactional
    public void deleteById(Long id) {
        DonationRecordModel model = getById(id);
        model.markAsRemoved();
        repository.save(model);
    }

    public DonationRecordModel getById(Long id) {
        return getById(id);
    }

    @Override
    protected BaseRepository<DonationRecordModel, Long> getRepository() {
        return repository;
    }
}
